/**
 * 试题辅助类，用于操作页面，以保持数据和页面一致
 */
var QuestionEditor = (function(){
    /**
	 * 构造函数
     * @param question  试题对象
     * @param titleId   题目的dom Id
     * @param optionListId  选项的dom Id
     * @constructor
     */
	function QuestionEditor(question,titleId,optionListId){
		this.question = question;
		this.titleId = titleId;
		this.optionListId = optionListId;
	}

	/**
	 * 初始化富文本框
	 */
	QuestionEditor.prototype.initTextBox = function(selector) {
		 $(selector).summernote({
             lang: 'zh-CN',
             toolbar:[
                 ['style',['bold','italic','underline','clear']],
                 ['fontsize',['fontsize']],
                 ['para',['ul','ol','paragraph']],
                 ['color',['color']],
                 ['insert', ['picture']],
             ]
		 });
	}

    /**
	 * 初始化题目输入框
     * @param title_selector
     */
    QuestionEditor.prototype.initTitle = function(){
		this.initTextBox(this.titleId);
	}

    /**
     * 设置题目
     */
    QuestionEditor.prototype.setTitle = function(titleText) {
        $("#" + this.titleId).val(titleText);
    }

    /**
	 * 添加一个选项框
     */
    QuestionEditor.prototype.addOptionBox = function(option){
        var label = option.label;
        var optionId = option.id;
        var optionScoreId = option.scoreId;
        var title = "";
        if(option.content) {
            title = option.content
        }

        var score = 0;
        if(option.score != 0) {
            score = option.score;
        }
		var temHtml = "";
		temHtml = `<div class="option_item">
								<div class="form-group">
									<label for="timu" class="col-sm-2 control-label">选项${label}：</label>
									<div class="col-sm-8">
										<textarea class="answer form-control" id="${optionId}">${title}</textarea>
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">分数：</label>
									<div class="col-sm-8">
										<input type="text" placeholder="分数" value="${score}" style="width:200px;" id="${optionScoreId}" class="form-control" />
									</div>
								</div>
						</div>`;

		$(this.optionListId).append(temHtml);
        // this.initTextBox("#"+ optionId);

	}

    /**
	 * 初始化选项
     */
    QuestionEditor.prototype.initOption = function(number){
    	for(let i = 0; i < number; i++) {
			this.addOption();
        }
	}

    /**
	 * 重置试题
     */
    QuestionEditor.prototype.resetQuestion  = function(){
    	this.question.resetQuestion();
    	$(this.optionListId).empty();
	}

    /**
	 * 添加试题选项
     */
	QuestionEditor.prototype.addOption = function(){
        var label = this.question.generateLabel();
        var id = this.question.boxPrefix + label;
        var optionScoreId = this.question.scorePrefix + label;
        var option = new Option();
        option.setId(id).setLabel(label);
        option.setScoreId(optionScoreId);
        this.question.addOption(option);
        console.info(this.question);
        //生成页面数据
        this.addOptionBox(option);
	}

    /**
     * 修改试题使用
     */
    QuestionEditor.prototype.setOption = function(opt){
        var label0 = this.question.generateLabel(); //用于报废序号
        var label = opt.label;
        var id = this.question.boxPrefix + label;
        var optionScoreId = this.question.scorePrefix + label;
        var option = new Option();
        option.setId(id).setLabel(label);
        option.setScoreId(optionScoreId);
        option.setContent(opt.content);
        option.setScore(opt.score);
        this.question.addOption(option);
        console.info(this.question);
        //生成页面数据
        this.addOptionBox(option);
    }

    /**
	 * 获取指定id富文本内容
     * @param id
     * @returns {jQuery}
     */
    QuestionEditor.prototype.getTextValue = function(id) {
    	console.info($(id).val());
        return $(id).val();
	}

    /**
	 * 判断富文本内容是否为空
     * @param value
     * @returns {boolean}
     */
    QuestionEditor.prototype.isEmpty = function(value){
        // var value = value.replaceAll("<p>","").replaceAll("</p>","").replaceAll("<br>","").trim();
        if(value == null) {
        	return true;
        }
        var value = value.replace(/<p>/g,"").replace(/<\/p>/g,"").replace(/<br>/g,"").trim();
        if(value.length == 0) {
        	return true;
		} else {
        	return false;
		}
	}
    /**
	 * 深拷贝
     */
    // function deepCopy(obj){
    //     if(typeof obj != 'object'){
    //         return obj;
    //     }
    //     var newobj = {};
    //     for ( var attr in obj) {
    //         newobj[attr] = deepCopy(obj[attr]);
    //     }
    //     return newobj;
	// }
    /**
	 * 去除掉无用的属性
     * @param question
     */
    QuestionEditor.prototype.getPureQuestion = function(){
		var question = this.question;
		for(var key in this.question) {
			if(key != "type" && key !="options" && key != "title" && key != "examinationPaperId" && key != "info") {
				delete question[key];
			}
		}

		var options = question.options;

		for(let i = options.length - 1; i >= 0 ; i--) {

			if((options[i].content.length) == 0) {
               options.splice(i,1);//删除空白元素
			} else {
				// 删除对象中多余属性
				for(let key in options[i]) {
					if(key != "label" && key != "score" && key != "content") {
						delete options[i][key];
					}
				}
			}
		}
		return question;
	}

	return QuestionEditor;
}());
